<!--
 * @Author: fjxcjm 3157329468@qq.com
 * @Date: 2024-10-08 15:11:13
 * @LastEditors: fjxcjm 3157329468@qq.com
 * @LastEditTime: 2024-10-12 14:24:53
 * @FilePath: \vue3-project\src\views\login\login.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div id="container">
    <div id="poster" class="back">
      <el-form
        class="login-container"
        label-position="left"
        label-width="0px"
        ref="loginForm"
      >
        <h3 class="login_title">培训机构管理系统</h3>

        <el-form-item label="" prop="username">
          <el-input
            v-model="username"
            type="text"
            autocomplete="off"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>

        <el-form-item label="" prop="password">
          <el-input
            v-model="password"
            type="password"
            autocomplete="off"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>

        <el-form-item label="">
          <el-radio-group v-model="selectedRole">
            <el-radio value="promoter">地推教师</el-radio>
            <el-radio value="teacher">授课教师</el-radio>
            <el-radio value="admin">管理员</el-radio>
            <el-radio value="parent">家长</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <el-button @click="handleLogin" :loading="loading" class="login-btn"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="footer-container">
      <footer class="footer">&copy; 2023 star培训机构管理系统</footer>
    </div>
  </div>
</template>

<script setup>
import { login } from "@/service/api/login"; // 导入登录API
import { ElMessage } from "element-plus";
import { ref } from "vue"; // 导入Vue的ref函数
import { useRouter } from "vue-router";

const username = ref(""); // 用户名
const password = ref(""); // 密码
const selectedRole = ref(""); // 选中的角色
const loading = ref(false); // loading状态
const router = useRouter();

function handleLogin() {
  if (!username.value || !password.value || !selectedRole.value) {
    ElMessage.warning("请输入账号、密码以及选择身份");
    return;
  }

  loading.value = true; // 开始 loading
  const data = {
    username: username.value,
    password: password.value,
    role: selectedRole.value,
  };

  login(data)
    .then(async (response) => {
      if (response.coding === 1) {
        localStorage.setItem("token", response.token);
        localStorage.setItem("userRole", selectedRole.value);
        ElMessage.success("登录成功");
        router.push("/");
      } else {
        ElMessage.error(response.message || "登录失败");
      }
    })
    .catch(() => {
      ElMessage.error("登录请求失败，请重试");
    })
    .finally(() => {
      loading.value = false; // 结束 loading
    });
}
</script>

<style lang="scss" scoped>
#poster {
  background-position: center;
  height: 100vh; // 减去 footer 高度
  width: 100vw;
  background-size: cover;
  background-repeat: no-repeat;
  background-image: url("@/assets/images/b.jpg");
}

body {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.login-container {
  border-radius: 15px;
  background: rgba(255, 255, 255, 0.5);
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  padding: 35px;
  margin: auto;
  width: 600px;
  min-height: 300px;
  position: absolute;
  top: 100px;
  left: 100px;

  .login_title {
    margin-bottom: 30px;
    text-align: center;
    color: #505458;
    font-size: larger;
    font-weight: bolder;
  }
  .login-btn {
    width: 100px;
    height: 30px;
    background-color: #88dba3;
  }
}

.back {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
}

.footer-container {
  .footer {
    text-align: center;
    padding: 10px;
    background-color: rgba(255, 255, 255, 0.2); // 白色透明背景
    position: fixed;
    bottom: 0;
    width: 100%;
    font-size: larger;
    color: #0b0606;
    font-weight: bolder;
  }
}
</style>
